home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / pas_all.zip / TI179.ASC < prev    next >
Text File  |  1991-09-11  |  5KB  |  199 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.   PRODUCT : TURBO PASCAL                               NUMBER : 179
  10.   VERSION : 3.0xx
  11.        OS : ALL
  12.      DATE : May 21, 1986                                 PAGE : 1/3
  13.     TITLE : ALTERNATIVE FILE COPY ROUTINE
  14.  
  15.  
  16.  
  17.  
  18.   program FileCopy;
  19.   {
  20.   This is a simple file copy program that illustrates how to use
  21.   BlockRead and BlockWrite to make an exact copy of any file.
  22.  
  23.   Note:     If you want to adapt this program so that it will  work
  24.             on the CP/M 80 or CP/M 86 versions of Turbo Pascal  you
  25.             must do the following:
  26.  
  27.    (1) Change the variables "SizeInBytes" and "LastByte" from  real
  28.        variables to integer variables.
  29.  
  30.    (2) Change calls to "LongFileSize" and "LongSeek" to  "FileSize"
  31.        and "Seek."
  32.  
  33.    (3) Take out the call to "Trunc(LastByte)" and change it  to
  34.        just "LastByte."
  35.   }
  36.   const
  37.     BufSize = 128;                  { The buffer SizeInBlocks }
  38.  
  39.   type
  40.                            { The type for the buffer variable }
  41.     BufType = array[1..BufSize] of byte;
  42.   var
  43.     Source, Dest : file;         { The input and output files }
  44.  
  45.     SourceName,             { The input and output file names }
  46.     DestName : string[14];
  47.  
  48.     Buffer : BufType;        { The buffer for block transfers }
  49.  
  50.     SizeInBytes,           { The size in bytes of source file }
  51.  
  52.     LastByte : real;       { The position of the last byte in }
  53.                            { the last partially full buffer   }
  54.                            { read from the source file.       }
  55.  
  56.     SizeInBlocks,          { The size in blocks of the source }
  57.                            { file                             }
  58.  
  59.     Count : integer;         { A general use counter variable }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.   PRODUCT : TURBO PASCAL                               NUMBER : 179
  76.   VERSION : 3.0xx
  77.        OS : ALL
  78.      DATE : May 21, 1986                                 PAGE : 2/3
  79.     TITLE : ALTERNATIVE FILE COPY ROUTINE
  80.  
  81.  
  82.  
  83.  
  84.     FileExists : boolean; { Flag to test if source file exits }
  85.  
  86.     ByteFile : file of byte;        { Used for byte transfers }
  87.  
  88.   begin { FileCopy }
  89.     Write('Copy from: ');
  90.     Readln(SourceName);
  91.  
  92.     { Calculate the size in bytes of }
  93.     { the source file                }
  94.     Assign(ByteFile, SourceName);
  95.     {$I-} Reset(ByteFile); {$I+}
  96.     FileExists := (IOResult = 0);
  97.     SizeInBytes := LongFileSize(ByteFile);
  98.     Close(ByteFile);
  99.  
  100.     if FileExists then
  101.     begin
  102.       { Open the source file as an }
  103.       { untyped file               }
  104.       Assign(Source, SourceName);
  105.       {$I-} Reset(Source); {$I+}
  106.       FileExists := (IOResult = 0);
  107.  
  108.       { Calculate the size of the }
  109.       { source file in blocks     }
  110.       SizeInBlocks := FileSize(Source);
  111.  
  112.       { Calculate the position of the   }
  113.       { last byte in the last partially }
  114.       { full buffer read with BlockRead }
  115.       LastByte := SizeInBytes -
  116.                ((SizeInBlocks-1) * 128.0);
  117.     end;
  118.     if FileExists then
  119.       begin
  120.         { Open the destination file }
  121.         Write('       To: ');
  122.         Readln(DestName);
  123.         Assign(Dest, DestName);
  124.         Rewrite(Dest);
  125.  
  126.         { Copy all the full buffers to }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.   PRODUCT : TURBO PASCAL                               NUMBER : 179
  142.   VERSION : 3.0xx
  143.        OS : ALL
  144.      DATE : May 21, 1986                                 PAGE : 3/3
  145.     TITLE : ALTERNATIVE FILE COPY ROUTINE
  146.  
  147.  
  148.  
  149.  
  150.         { the destination file         }
  151.         for Count := 1 to SizeInBlocks-1 do
  152.         begin
  153.           BlockRead(Source, Buffer, 1);
  154.           BlockWrite(Dest, Buffer, 1);
  155.         end;
  156.         { Read the last partially full }
  157.         { buffer                       }
  158.         BlockRead(Source, Buffer, 1);
  159.  
  160.         { Close up the open files }
  161.         Close(Source);
  162.         Close(Dest);
  163.  
  164.         { Open the destination file as a   }
  165.         { file of byte and seek to the end }
  166.         Assign(ByteFile, DestName);
  167.         Reset(ByteFile);
  168.         LongSeek(ByteFile, LongFileSize(ByteFile));
  169.  
  170.         { Append the partially full buffer }
  171.         { to the end of the destination    }
  172.         { file and then close it           }
  173.         for Count := 1 to Trunc(LastByte) do
  174.           Write(ByteFile, Buffer[Count]);
  175.         Close(ByteFile);
  176.       end
  177.  
  178.     else
  179.       { Issue an error message if an }
  180.       { error occured on the reset   }
  181.       Writeln('An error occurred when resetting "', SourceName, '".');
  182.   end. { FileCopy }
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.